<?php 
$sys_pages = unserialize(SYS_PAGES);
$search = Utils::get_param('search', '');
$exit = Utils::get_param('__exit', 0);
if ($exit == 1) {
  unset($_SESSION['user']);
  unset($_SESSION[SYS . $app_data->sys->php_name]);
  setcookie('user', '', time() - 3600);
  $url = str_replace('&__exit=1', '', Utils::get_url());
  $url = str_replace('?__exit=1', '', $url);
  header('location: ' . $url);
  return;
}

if (!isset($_SESSION['profile'])) {
  $rslt = $orm->get('pb_ht_admin', NULL, NULL, NULL, NULL, NULL)->d;
  if (count($rslt) > 0) {
    $_SESSION['profile'] = $rslt[0];
  }
}

$user = null;
if (isset($_SESSION['user'])) {
  $user = json_decode($_SESSION['user']);
}

$cart = array();
if (isset($_SESSION['cart'])) {
  $cart = json_decode($_SESSION['cart']);
}

$opts = new stdClass();
$opts->cd = 't.page_id = :v1';
$opts->cdv = array(':v1' => $app_data->contents[$app_data->query]->id);
$cs_cats= $orm->get('pb_ht_category', 't.order', 'ASC', NULL, NULL, $opts)->d;
?>

<!DOCTYPE html>
<html>
  <head>
    <title><?php echo ((isset($_SESSION['profile'])) ? $_SESSION['profile']->shop_name : ''); ?></title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0" />
    <?php include '../include/common.inc.php'; ?>
    <style type="text/css"><?php echo $app_data->contents[$app_data->query]->css; ?></style>
    
    <?php 
      if (!isset($app_data->contents[$app_data->query]->__iframe) || $app_data->contents[$app_data->query]->__iframe == 0) {
        $hdfile = str_replace('.php', '', $app_data->contents[$app_data->query]->html) . '.hd.php';
        if (file_exists($hdfile)) {
          include $hdfile;
        }
      }
    ?>
    
    <link rel="stylesheet" href="<?php echo TH_DIR . basename(dirname(__FILE__)); ?>/megamenu/sky-mega-menu.css" />
    <link rel="stylesheet" href="<?php echo TH_DIR . basename(dirname(__FILE__)); ?>/megamenu/sky-mega-menu-black.css" />
    
    <!--[if lt IE 9]>
      <link rel="stylesheet" href="<?php echo TH_DIR . basename(dirname(__FILE__)); ?>/megamenu/sky-mega-menu-ie8.css">
      <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->
    
    <!--[if lt IE 10]>
      <script type="text/javascript" src="<?php echo TH_DIR . basename(dirname(__FILE__)); ?>/megamenu/jquery.placeholder.min.js"></script>
    <![endif]-->
    
    <style>
      #cs_catbar h3 {
        font-size: 13px !important;
        font-family: Trebuchet MS !important;
      }
      #cs_catbar span {
        font-size: 12px !important;
        font-family: Trebuchet MS !important;
      }
    </style>
  </head>

  <body class="pure-skin-mine">

    <div class="cs_toolbar_wrapper">
      <div class="cs_toolbar">
        <img style="height: 55px; margin-top: 5px;" src="<?php echo TH_DIR . basename(dirname(__FILE__)); ?>/skin/images/logo.png" alt="logo"></img>
        <a class="cs_btn_li" id="cs_login" <?php if (isset($_SESSION['user'])) { ?>style="display: none;"<?php } ?> onclick="return Url.redirect(____redmd, ____sys + '.php?i=' + ____login_id + '&u=' + Utils.encodeURI(document.URL));" href="#">登录</a>
        <a class="cs_btn_reg" id="cs_register" <?php if (isset($_SESSION['user'])) { ?>style="display: none;"<?php } ?> onclick="return Url.redirect(____redmd, ____sys + '.php?i=' + ____login_id + '&u=' + Utils.encodeURI(document.URL));" href="#">注册</a>
        <a class="cs_btn_acct" id="cs_account" <?php if (!isset($_SESSION['user'])) { ?>style="display: none;"<?php } ?> href="#" onclick="return Url.redirect(____redmd, ____sys + '.php?i=' + <?php echo ACCT_ID; ?>);">我的账号</a>
        <a class="cs_btn_lo" id="cs_logout" <?php if (!isset($_SESSION['user'])) { ?>style="display: none;"<?php } ?> onclick="Utils.block_ui(); location.href=document.URL.replace('#', '') + ((document.URL.indexOf('?') == -1) ? '?__exit=1' : '&__exit=1'); return false;" href="#">注销</a>
      </div>
    </div>
    <div>&nbsp;</div>
    
    <div class="cs_header">
      <div class="cs_menubar"></div>
    </div>
    <div class="cs_wrapper">
      <div style="display: none;"><div class="cs_navbar ui-widget-header ui-corner-all" style="display: none;"></div></div>
      <div id="cs_adtop">
        <?php if ($app_data->contents[$app_data->query]->ad_top != '') { ?>
        <img src="<?php echo $app_data->contents[$app_data->query]->ad_top; ?>" alt="" style="width: 950px;"></img>
        <?php } ?>
      </div>
      <table style="width: 100%;" cellpadding=0 cellspacing=0>
        <tr>
          <td id="cs_catbar" style="width: 200px; vertical-align: top; padding-right: 10px;">
            <div class="ui-widget-content ui-corner-all"><ul class="modern-menu mm-vertical"></ul><div>&nbsp;</div></div>
          </td>
          <td style="vertical-align: top;">
            <div class="cs_body">
              <?php 
                if (isset($app_data->contents[$app_data->query]->__iframe) && $app_data->contents[$app_data->query]->__iframe == 1) {
                  $sys_pages = unserialize(SYS_PAGES);
                  if (in_array($app_data->param_id, $sys_pages)) {
                    echo $app_data->contents[$app_data->query]->html;
                  } else {
                    echo '<div class="cs_custom">' . $app_data->contents[$app_data->query]->html . '</div>';
                  }
                } else {
                  if (file_exists($app_data->contents[$app_data->query]->html)) {
                    include $app_data->contents[$app_data->query]->html;
                  }
                }
              ?>
            </div>
          </td>
        </tr>
      </table>
      
      <div id="cs_adbottom">
        <?php if ($app_data->contents[$app_data->query]->ad_bottom != '') { ?>
        <img src="<?php echo $app_data->contents[$app_data->query]->ad_bottom; ?>" alt="" style="width: 950px;"></img>
        <?php } ?>
      </div>
    </div>
    <div class="cs_f_up">&nbsp;</div>
    <div class="cs_f_bg">
      <div>&nbsp;</div>
      <?php include 'inc.foot.php'; ?>
      <div>&nbsp;</div>
      <?php echo $_SESSION['profile']->footer; ?>
    </div>
    
    <script type="text/javascript">
      /*<![CDATA[*/
      var ____user = JSON.parse('<?php echo addslashes(json_encode($user)); ?>');
      var ____sys_pages = JSON.parse('<?php echo addslashes(json_encode($sys_pages)); ?>');
      var ____search = '<?php echo addslashes($search); ?>';
      var ____cats = JSON.parse('<?php echo addslashes(json_encode($cs_cats)); ?>');
      
      $(function(){
        ____toolbar();
      });
      
      function ____func(parsed_url, rslt, elm) {
        var id = parsed_url.queryKey.i ? parsed_url.queryKey.i : ____default_id;
        
        var bkup = rslt;
        rslt = Utils.parse(rslt);
        if (rslt == '') {
          rslt = {
            html: bkup,
            ad_top: '',
            ad_bottom: ''
          }
          ____cats = [];
        } else {
          ____cats = rslt.__cats
        }
        
        var file = parsed_url.queryKey.__file ? parsed_url.queryKey.__file : 'index';
        if (Utils.exist(id, ____sys_pages)) {
          elm.html(rslt.html);
        } else {
          elm.html('<div class="cs_custom">' + rslt.html + '</div>');
        }
        
        $('#cs_adtop').empty();
        $('#cs_adbottom').empty();
        if (rslt.ad_top != '') {
          $('#cs_adtop').html('<img src="' + rslt.ad_top + '" alt="" style="width: 950px;"></img>');
        }
        if (rslt.ad_bottom != '') {
          $('#cs_adbottom').html('<img src="' + rslt.ad_bottom + '" alt="" style="width: 950px;"></img>');
        }
        
        $(document).tooltip();
        $('#glb_serach').val(____search);
        ____menubar(id, ____root)
        ____leftbar(id, file);
        ____nav(id, file);
        ____toolbar();
      };

      function ____toolbar() {
        if (!____user) {
          $('#cs_login').show();
          $('#cs_register').show();
          $('#cs_account').hide();
          $('#cs_logout').hide();
        } else {
          $('#cs_login').hide();
          $('#cs_register').hide();
          $('#cs_account').show();
          $('#cs_logout').show();
        }
      };

      function ____menubar(id, root) {
        var mmid = ____mmid(root, id);
        $(".cs_menubar").empty();
        $('.cs_menubar').append('<ul id="j_menu" class="sky-mega-menu sky-mega-menu-anim-scale sky-mega-menu-response-to-stack"></ul>');
        ____menu(root, $('#j_menu'), mmid);
        $("#j_menu").append(
          '<li class="right search">' +
            '<form action="#">' +
              '<div class="input">' +
                '<button type="submit" class="button" onclick="____search = $(\'#glb_serach\').val(); Url.redirect(____redmd, ____sys + \'.php?i=<?php echo ART_ID; ?>&search=\' + Utils.encodeURI(____search));">Go</button>' +
                '<input id="glb_serach" type="text" placeholder="Search" value="' + ____search + '">' +
              '</div>' +
            '</form>' +
          '</li>'
        );
        $('form').submit(false);

        if (____cats.length == -1 || id == 22 || id == 10 || id == 16 || id == 17 || id == 18 || id == 19) {
          $('#cs_catbar').hide();
        } else {
          $('#cs_catbar').show();
          $('#cs_catbar .modern-menu').empty();
          $("#cs_catbar .modern-menu").append('<h3 style="padding: 5px 0 0 10px; margin: 0 0 10px 0;">相关栏目</h3>');
          for (var idx in ____cats) {
            var itm = ____cats[idx];
            $('#cs_catbar .modern-menu').append('<li><a href="#" onclick="return Url.redirect(' + ____redmd + ', \'' + ____sys + '.php?i=<?php echo ART_ID; ?>&cat=' + itm.id + '\');"><span>' + Utils.get_short(itm.name, 10) + '</span></a></li>');
          }
          $('#cs_catbar .modern-menu').modernMenu();
        }
      };
      
      function ____menu(node, elm, mmid) {
        for (var idx in node.__c) {
          var itm = node.__c[idx];
          elm.append('<li' + ((mmid == itm.id) ? ' class="cs_hi"' : '') + ' value="' + itm.id +'">' +
                       '<a href="' + ____sys + '.php?i=' + itm.id + '" onclick="return Url.redirect(' + ____redmd + ', \'' + ____sys + '.php?i=' + itm.id + '\');">' + ((itm.icon == '') ? '' : '<i class="fa fa-fw fa-lg">' + itm.icon + '</i>') + itm.name + '</a>' +
                       ((itm.__c.length > 0) ? '<div class="grid-container3"><ul id="pgul_' + itm.id + '"></ul></div>' : '') + 
                     '</li>');
          if (itm.__c.length > 0) {
            ____menu(itm, $('#pgul_' + itm.id), mmid);
          }
        }
      };
      /*]]>*/
    </script>
    <?php include '../include/inc.theme.js.php'; ?>
  </body>
</html>